<template>
  <div class="my-container">
        <!-- 头像 已登录头部-->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <!-- 左边头像 -->
        <div class="left">
          <van-image
            class="avanet"
            :src="userInfo.photo"
            round
            fit="cover"
          />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <!-- 右边 -->
        <div class="right">
          <van-button
          to="/user/profile"
          round
          size="mini">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
<!-- 未登录 -->
    <div class="header not-login" v-else>
      <div class="layout-btn" @click="$router.push('/login')">
        <img src="../../assets/mobile.png" alt="" class="mobile" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 导航 -->
    <div>
      <van-grid column-num="2" class="grid-nav" clickable>
        <van-grid-item class="grid-item">
          <i slot="icon" class="toutiao toutiao-shoucang"></i>
          <span slot="text" class="text">收藏</span>
        </van-grid-item>

        <van-grid-item class="grid-item">
          <i slot="icon" class="toutiao toutiao-lishi"></i>
          <span slot="text" class="text">历史</span>
        </van-grid-item>
      </van-grid>
    </div>

      <van-cell-group>
  <van-cell title="消息通知" is-link />
  <van-cell class="mb-9" title="小智同学" is-link />
  <van-cell v-if="user" class="loyout-cell" title="退出登录"
  @click="onLogout"
  clickable
  />
</van-cell-group>

  </div>
</template>

<script>
// stats的语法糖,当我们一个组件需要多个状态的时候
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },
  // 加载数据
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  name: 'WoDe',
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 退出提示, 确认退出,清除登录状态(容器中的user + 本地存储的user)
    onLogout () {
      this.$dialog.confirm({
        title: '确认退出吗?'
      })
        .then(() => {
          // 确认退出,清除登录状态
          this.$store.commit('setUser', null)
          // console.log('确认的执行')
        })
        .catch(() => {
          console.log('取消的执行')
        })
    },
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
      } catch (err) {
        this.$toast('获取失败')
      }
    }
  }

}
</script>

<style scoped lang="less">
.my-container {
  .loyout-cell{
          color : red;
          text-align: center;
  }
  .header {
    height: 361px;
    background: url("../../assets/banner.png"); //~@ 等同于 ../../
    background-size: cover; //完整的缩放在整个盒子中
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .layout-btn {
      flex-direction: column;
      display: flex;
      justify-content: center;
      align-items: center;
      .mobile {
        width: 132px;
        height: 132px;
        margin-bottom: 28px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      // background-color: pink;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between; //往两边撑开
      align-items: center; //垂直居中
      .left {
        display: flex;
        align-items: center;
        .avanet {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 5px solid #fff;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      height: 130px;
      // background-color: red;
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .count {
          font-size: 36px;
          color: #fff;
        }
        .text {
          font-size: 23px;
          color: #fff;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 141px;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
}
</style>
